import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/base/base_navigation_view.dart';

class BaseGetView<T> extends GetView<T> {
  // 标题
  final String? title = "";
  // 标题文字颜色
  final Color? titleColor = null;
  // 标题组件
  final Widget? titleView = null;

  // 导航栏背景色
  final Color? barBackgroundColor = null;
  // 导航栏背景是否透明
  final bool? barTransparent = false;
  // 当透明度为0时，是否隐藏导航栏
  final bool? hideBarWhenOpacity = false;
  // 是否隐藏导航栏阴影
  final bool? hideNavShadow = true;
  // 透明度(可变)
  final RxDouble? barOpacity = 1.0.obs;
  // 内容上移置顶（标题栏透明时用到）
  final bool? extendBodyBehindBar = false;

  // 控制器背景色
  final Color? backgroundColor = null;
  // 页面主体
  final Widget _body = const SizedBox.shrink();

  // 隐藏返回按钮
  final bool? hidenBack = false;
  // 返回按钮
  final Widget? backView = null;
  // 返回按钮颜色
  final Color? navBackViewColor = null;

  // 右侧按钮
  final List<Widget>? rightViews = [];

  // 点击空白处隐藏键盘
  final bool? hiddenKeyboardWhenTouch = true;

  // 悬浮按钮
  final Widget? floatingActionButton = null;
  // 悬浮按钮位置
  final FloatingActionButtonLocation? floatingActionButtonLocation = null;
  // 悬浮按钮动画
  final FloatingActionButtonAnimator? floatingActionButtonAnimator = null;

  // 这个小部件出现在appBar的底部
  final PreferredSizeWidget? bottomView = null;
  // 页面底部组建
  final Widget? bottomNavigationBar = null;

  /// 在 Flutter 中 Scaffold 默认情况下 resizeToAvoidBottomInset 为 true，
  /// 当 resizeToAvoidBottomInset 为 true 时，Scaffold 内部会将 mediaQuery.viewInsets.bottom 参与到 BoxConstraints 的大小计算，也就是键盘弹起时调整了内部的 bottom 位置来迎合键盘。
  final bool? resizeToAvoidBottomInset = true;

  // 点击空白处隐藏键盘
  final bool? dismissOnTap;

  // 如何处理点击事件
  final HitTestBehavior? behavior;

  //  是否排除语义
  final bool? excludeSemantics;

  // 是否深色模式
  final bool? isDarkMode = true;

  BaseGetView({
    super.key,
    this.dismissOnTap = false,
    this.behavior,
    this.excludeSemantics = false,
  });

  @override
  Widget build(BuildContext context) {
    return BaseNavigationView(
      title: title,
      titleView: titleView,
      titleColor: titleColor,
      barBackgroundColor: barBackgroundColor,
      hideNavShadow: hideNavShadow,
      barTransparent: barTransparent,
      extendBodyBehindBar: extendBodyBehindBar,
      backView: backView,
      rightViews: rightViews,
      hidenBack: hidenBack,
      backgroundColor: backgroundColor,
      body: buildBody(context),
      floatingActionButton: floatingActionButton,
      floatingActionButtonLocation: floatingActionButtonLocation,
      floatingActionButtonAnimator: floatingActionButtonAnimator,
      resizeToAvoidBottomInset: resizeToAvoidBottomInset,
      bottomView: bottomView,
      bottomNavigationBar: bottomNavigationBar,
      hiddenKeyboardWhenTouch: hiddenKeyboardWhenTouch,
      barOpacity: barOpacity,
      hideBarWhenOpacity: hideBarWhenOpacity,
      isDarkMode: isDarkMode,
    );
  }

  Widget buildBody(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if (dismissOnTap!) _dismissKeyboard(context);
      },
      behavior: behavior,
      excludeFromSemantics: excludeSemantics ?? false,
      child: Container(
        color: backgroundColor,
        child: _body,
      ),
    );
  }

  void _dismissKeyboard(BuildContext context) {
    debugPrint("dismissKeyboard");
    final currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      currentFocus.focusedChild?.unfocus();
    }
  }
}
